package com.sanyue.thread;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author sanyue
 * @version 1.0
 * @description: TODO
 * @date 2022/2/8 下午8:21
 */
public class ThreadTest {

    @Test
    public void testThread() throws Exception {
        //Runnale
        Runnable threadRun = new ThreadStudy.ThreadRun();
        new Thread(threadRun).start();

        //流式编程写法实现Runnable
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("new Runnable--<");
            }
        });
        t.start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
        }).start();

        //Callable
        ThreadStudy.ThreadCall<String> stringThreadCall = new ThreadStudy.ThreadCall<>();
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Future<String> future = executorService.submit(stringThreadCall);
        String s = future.get();
        System.out.println(s);
        executorService.shutdown();

    }

    //volatile  1.可见性 2.禁止指令重排  （不保证原子性）
    static int num = 1;
    private int flag = 0;

    @Test
    public void test() {

    }
}


